﻿using System;
using System.Collections.Generic;
using DynamicLoader;
using DynamicLoader.DataStructures;
using System.Windows;
namespace SSCCMembership
{
    public class ExternalDependanciesLoader:IDependanciesLoader
    {

        #region IDependanciesLoader Members

        public List<Dependancy> LoadDependancies(XAPStream XAPStream)
        {
            try
            {
                List<Dependancy> Dependancies = new List<Dependancy>();
                var File = XAPStream.LoadTextFile("Dependancies.txt");
                //Read Header Line
                File.ReadLine();
                //Load First Line
                string CurrentLine = File.ReadLine();
                while (CurrentLine != "End:Of:File")
                {
                    var Values = CurrentLine.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    XAPName XAPN = Values[0];
                    AssemblyName ASSN = Values[1];
                    int VER = int.Parse(Values[2]);
                    var Dep = new Dependancy(XAPN, new AssemblyName[]{ ASSN}, VER);
                    Dependancies.Add(Dep);
                    CurrentLine = File.ReadLine();
                }
                return Dependancies;
            }
            catch (Exception ex)
            {
                //In the event that there is no file we assume that there are no dependancies
                //MessageBox.Show(ex.Message);
                throw new Exception("There was an error decoding the formating of the DEpendancies File.", ex);
                //throw new Exception("There was an error decoding the formating of the DEpendancies File.", ex);
            }
        }

        #endregion
    }
}
